package com.fan.teamer.security;

import javax.inject.Inject;
import javax.inject.Named;

import org.springframework.dao.DataAccessException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.transaction.annotation.Transactional;

import com.fan.teamer.model.dao.MemberDao;
import com.fan.teamer.model.domain.Member;

@Named("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {

	@Inject
	@Named("memberDao")
	private MemberDao memberDao;
	@Inject
	@Named("assembler")
	private Assembler assembler;

	public MemberDao getMemberDao() {
		return memberDao;
	}

	public void setMemberDao(MemberDao memberDao) {
		this.memberDao = memberDao;
	}

	public Assembler getAssembler() {
		return assembler;
	}

	public void setAssembler(Assembler assembler) {
		this.assembler = assembler;
	}

	@Transactional(readOnly = true)
	public UserDetails loadUserByUsername(String username)
			throws UsernameNotFoundException, DataAccessException {

		// UserDetails userDetails = null;
		Member member = memberDao.getMember(username);
		if (member == null)
			throw new UsernameNotFoundException("user not found");

		return assembler.buildUserFromUserEntity(member);
	}
}